/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import gridHeader from "@/components/print/selectTemplet.vue"
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    onInit() {
      this.queryFields = ["Client_name", "Client_type"]
    },
    async onInited() {
      if (this.$route.path == "/q_client" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     const field = x.field
        //     if (x.type == "like") {
        //       this.searchFormFields[field] = null
        //     }
        //     if (x.type == "select") {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        // this.searchFormFields.Client_no = null
        // this.searchFormFields.Client_name = null
        // this.searchFormFields.Client_type = null
        // this.searchFormFields.Client_flag = null
        // const selectList = ['Client_type','Client_flag',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        setTimeout(() => {
          this.refresh()
        })
        // setTimeout(()=>{
        //   this.$route.query={}
        // },2000)
      }
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      this.single = true
      if (this.$route.path != "/q_client") {
        this.setFiexdSearchForm(true);
        this.height = 550 - 150;
        this.buttons.forEach(item => {
          if (item.value == "Search" ) {
            item.hidden = false
          } else {
            item.hidden = true
          }
        })
        if (this.$route.path == "/q_goods_info" || this.$route.path == "/q_purchase" || this.$route.path == "/q_stock_in") {
          this.searchFormFields.Client_type = "1"
          this.searchFormOptions.forEach(item => {
            item.forEach(x => {
              if (x.field == "Client_type") {
                x.disabled = true
              }
            })
          })
        }

      }
      // this.setFiexdSearchForm(true);
      // this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })

      // this.http.post("api/q_client/getPageData", {}, true).then(reslut => {
      //   console.log(reslut.rows);
      // })
      // setTimeout(()=>{
      //   let rows = document.querySelectorAll(".vol-table .el-table__body-wrapper .el-table__row")
      // console.log("rows",rows);
      // },100)
    },
    async searchBefore(param) {
      console.log(this.searchFormOptions)
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path != "/q_client") {
        param.wheres = param.wheres.filter(item => item.name != "Client_id")
        this.$route.query = {}
      }
      if (this.$route.path == "/q_goodsIn_class") param.wheres = `[{"name":"Client_type","value":"1","displayType":"select"}]`
      if (this.$route.path == "/q_client" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Client_id", param)
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     const field = x.field
        //     if (x.type == "like") {
        //       if( this.searchFormFields[field]){
        //         param.wheres = param.wheres.filter(item => item.name != "Client_id")
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })
        // if (this.searchFormFields.Client_no || this.searchFormFields.Client_name) {
        //   param.wheres = param.wheres.filter(item => item.name != "Client_id")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    async searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      // result = await result.filter((item) => {
      //   return item.Client_type != 0;
      // });

      return true;
    },
    addBeforeAsync(formData) {
      this.rowAuditStatus = 0
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return this.testAdd(formData);
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.testAdd(formData);
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => { return x.AuditStatus == 1 });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row);
    },
    async modelOpenBefore(row) {
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      this.editFormOptions.forEach(item => {
        item.forEach(x => {

          if (this.$route.path != "/q_client") {//|| this.rowAuditStatus != 0 || !this.CreateUserBoole
            x.disabled = true
            this.boxButtons[0].hidden = true

          } else {
            this.boxButtons[0].hidden = false
            x.disabled = false
          }
          if (x.field == "Client_no") {
            x.disabled = true
          }
          if (!this.editFormFields.Province) {
            if (x.field == "City" || x.field == "District") {
              x.disabled = true
            }
          } else {
            if (x.field == "City") {
              let url = `api/Sys_Dictionary/getcRegionDictionary?sjid=${this.editFormFields.Province}`;
              this.http.post(url, {}).then((result) => {
                let CityList = result.map(a => {
                  return {
                    key: a.citys_Id,
                    value: a.citys_name
                  }
                })
                x.data = CityList
              });
            }
          }
          if (!this.editFormFields.City) {
            if (x.field == "District") {
              x.disabled = true
            }
          } else {
            if (x.field == "District") {
              let url = `api/Sys_Dictionary/getcRegionDictionary?sjid=${this.editFormFields.City}`;
              this.http.post(url, {}).then((result) => {
                let DistrictList = result.map(a => {
                  return {
                    key: a.citys_Id,
                    value: a.citys_name
                  }
                })
                x.data = DistrictList
              });
            }
          }
          if (x.field == "Province") {
            let url = `api/Sys_Dictionary/getcRegionDictionary?sjid=D39479C4-1A27-43BE-8B27-739EB5CEA278`;
            this.http.post(url, {}).then((result) => {
              let ProvinceList = result.map(a => {
                return {
                  key: a.citys_Id,
                  value: a.citys_name
                }
              })
              x.data = ProvinceList
            });
            x.onChange = (val, option) => {
              this.editFormOptions.forEach(e => {
                e.forEach(i => {
                  if (i.field == "City" || i.field == "District") {
                    this.editFormFields.City = null
                    this.editFormFields.District = null
                    i.disabled = true
                  }
                  if (val) {
                    if (i.field == "City") {
                      url = `api/Sys_Dictionary/getcRegionDictionary?sjid=${val}`;
                      this.http.post(url, {}).then((result) => {
                        let CityList = result.map(a => {
                          return {
                            key: a.citys_Id,
                            value: a.citys_name
                          }
                        })
                        if (i.field == "City") {
                          i.data = CityList
                        }
                      });
                      i.disabled = false

                    }
                  }
                })
              })
            }
          }
          if (x.field == "City") {
            x.onChange = (CityVal) => {
              this.editFormOptions.forEach(c => {
                c.forEach(t => {
                  if (t.field == "District") {
                    this.editFormFields.District = null
                    t.disabled = true
                    if (CityVal) {
                      let url = `api/Sys_Dictionary/getcRegionDictionary?sjid=${CityVal}`;
                      this.http.post(url, {}).then((result) => {
                        console.log("District", result);
                        let DistrictList = result.map(a => {
                          return {
                            key: a.citys_Id,
                            value: a.citys_name
                          }
                        })
                        t.data = DistrictList
                        t.disabled = false
                      });
                    }
                  }
                })
              })
            }
          }
        })
      })

    },
    testAdd(formData) {
      if (!this.editFormFields.Client_flag) {
        this.base.ElMessageFn('请选择客户标志')
        return false
      } if (!this.editFormFields.Province) {
        this.base.ElMessageFn('请选择省')
        return false
      } if (!this.editFormFields.City) {
        this.base.ElMessageFn('请选择市')
        return false
      } if (!this.editFormFields.District) {
        this.base.ElMessageFn('请选择区')
        return false
      } if (!this.editFormFields.Character) {
        this.base.ElMessageFn('请选择客户性质')
        return false
      }if (!this.editFormFields.Client_tel) {
        this.base.ElMessageFn('请填写客户电话')
        return false
      }
      return true
    },
  }
};
export default extension;
